//
//  VisitorView.swift
//  SwiftWB
//
//  Created by 吴志勇 on 2019/6/14.
//  Copyright © 2019 wzy. All rights reserved.
//

import UIKit

protocol VisitorViewDelegate: NSObjectProtocol {
    func visitorViewDidClickLoginBtn(visiot:VisitorView)
    func visitorViewDidClickRegisterBtn(visiot:VisitorView)
}

class VisitorView: UIView {

    
    //转盘标签
    @IBOutlet weak var rotationView: UIImageView!
    
    
    //图片标签
    @IBOutlet weak var iconImageView: UIImageView!
    
    //文本标签
    @IBOutlet weak var titleLabel: UILabel!
    
    //登录按钮
    @IBOutlet weak var loginButton: UIButton!
    //注册按钮
    @IBOutlet weak var registerButton: UIButton!
    
    
    
    
    //登录按钮事件
    @IBAction func loginBtnClick(_ sender: Any) {
        delegate?.visitorViewDidClickLoginBtn(visiot: self)
    }
    
    //注册按钮事件
    @IBAction func registerBtnClick(_ sender: Any) {
        delegate?.visitorViewDidClickRegisterBtn(visiot: self)
    }
    weak var delegate: VisitorViewDelegate?
    
    func setupVisitorInfo(imageName:String?, title:String){
        titleLabel.text = title
        guard let name = imageName else {
            //执行转盘动画
            statrtAniamtion()
            return
        }
        
        rotationView.isHidden = true
        iconImageView.image = UIImage(named: name)
        
        
    }
    
    private func statrtAniamtion(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.duration = 5.0
        anim.repeatCount = MAXFLOAT
        //注意 默认情况下只要视图消失，系统就会自动移除动画
        anim.isRemovedOnCompletion = false
        rotationView.layer.add(anim, forKey: nil)
    }
    
    class func visitorView() ->VisitorView{
        return Bundle.main.loadNibNamed("VisitorView", owner: nil, options: nil)?.last as! VisitorView
    }
}
